package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {

    @Autowired
    private ClueService clueService;
    @Log
    @PostMapping
    public Result add(@RequestBody Clue clue) {
        log.info("添加线索: {}", clue);
        clue.setStatus(1); // 待分配
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clueService.save(clue);
        return Result.success();
    }

    @GetMapping
    public Result list(ClueDto clueDto) {
        log.info("线索列表查询: {}", clueDto);
        PageResult<Clue> pageResult = clueService.listClues(clueDto);
        return Result.success(pageResult);
    }
    @Log
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId, @PathVariable Integer userId) {
        log.info("分配线索: {},{}", clueId, userId);
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setStatus(2); // 跟进中
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询线索: {}", id);
        Clue clue = clueService.getClueById(id);
        return Result.success(clue);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Clue clue) {
        log.info("跟进线索: {}", clue);
        clueService.trackClue(clue);
        return Result.success();
    }

    @Log
    @PutMapping("/false/{id}")
    public Result falseUpdate(@PathVariable Integer id, @RequestBody MarkFalseClueDto markFalseClueDto){
        log.info("划分伪线索: {},{}", id, markFalseClueDto);
       clueService.trackFakeClue(id, markFalseClueDto);
       return Result.success();
    }
    @Log
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id){
        log.info("线索转商机: {}", id);
        clueService.toBusiness(id);
        return Result.success();
    }
    @GetMapping("/pool")
    public Result getPool(ClueDto clueDto) {
        log.info("线索池查询");
        PageResult<Clue> pageResult = clueService.listCluesPool(clueDto);
        return Result.success(pageResult);
    }
}
